﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;

namespace GTS.Channel.Api {
    /// <summary>
    /// 彩游SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class CaiYou : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "CaiYou";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {
            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.IsHidden = false;
            info.Price = 10;
            info.DeviceCode = "GENERICDEVICE";

            //接收值
            info.Mobile = Request2.GetQ("mobile").Trim();
            info.ReceiveQuery = Request2.GetQ("msg").Trim();
            info.LinkID = Request2.GetQ("fakeid").Trim();
            info.LongPhone = Request2.GetQ("spnumber").Trim();
            info.Price = (Request2.GetQInt("feecode", 10) / 10).IfLessThan(10, 10);

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            string[] infos = info.ReceiveQuery.Split('_');
            if (infos.Length >= 4) {
                info.MountCode = infos[1].ToInt();
                info.GameCode = infos[2].PadLeft(5, '0');
                info.ChargeCode = infos[3];
            } else {
                info.MountCode = 493;
                info.GameCode = "00999";
                info.ChargeCode = "9";
            }

            //如果数据库中存在价格以数据库里为主
            ChannelInfo extInfo = Base.GetChannelInfo(SPCode, info.LongPhone, info.ReceiveQuery);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }

            return info;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            return null;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;

            //接收值
            info.LinkID = Request2.GetQ("fakeid").Trim();
            info.ResponseStatus = Request2.GetQ("status") == "DELIVRD" ? 1 : 0;
            info.MRDate = DateTime.Now;

            if (info.LinkID.IsNullEmpty()) return null;

            return info;
        }
        /// <summary>
        /// IVR的参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            //初始值
            IVRDealData info = new IVRDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.ResponseStatus = 1;

            //接收值
            info.Mobile = Request2.GetQ("mobile").Trim();
            info.FeeTime = Request2.GetQInt("timelong", 1);
            info.LinkID = Request2.GetQ("linkid").Trim();
            info.LongPhone = Request2.GetQ("spnumber").Trim();
            string starttime = Request2.GetQ("starttime").Trim();
            string endtime = Request2.GetQ("endtime").Trim();
            if (starttime.Length == 14) info.BeginTime = DateTime.ParseExact(starttime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            if (endtime.Length == 14) info.EndTime = DateTime.ParseExact(endtime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.LongPhone.IsNullEmpty() || info.FeeTime.Value < 1) return null;

            //如果数据库中存在价格以数据库里为主
            IVRChannelInfo extInfo = Base.GetIVRChannelInfo(SPCode, info.LongPhone);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }
            info.Price = info.FeeTime * info.Price;

            return info;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res.ToString());
        }
    }
}

